﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using DevExpress.Xpo.Metadata;
using System.Data;

namespace GDIT.Model
{
    class ObjectHelper
    {
        /// <summary>
        /// 检查业务对象是否存在
        /// </summary>
        /// <param name="per">业务对象</param>
        /// <returns>若是无此数据表对象返回False，否则返回True</returns>
        public static Boolean DBexist(PersistentClass per)
        {
            if (per.OID == -1) { return false; }
            string table = per.ClassInfo.TableName;

            IDbCommand command;
            command = per.Session.Connection.CreateCommand();
            //是否有该表
            command.CommandText = string.Format("select count(*) from INFORMATION_SCHEMA.TABLES where TABLE_NAME ='{0}' and TABLE_TYPE in ('BASE TABLE', 'VIEW')", table);
            if (Convert.ToInt32(command.ExecuteScalar()) == 0) { return false; }

            //是否唯一
            command.CommandText = String.Format("SELECT Count(*) From {0} Where Oid = {1} ", table, per.OID);
            return (Convert.ToInt32(command.ExecuteScalar()) > 0);
        }

        /// <summary>
        /// 检查业务对象（数据表）某字段的唯一性
        /// </summary>
        /// <param name="per">业务对象</param>
        /// <param name="FieldName">字段名</param>
        /// <param name="Value">值</param>
        /// <returns>若是唯一或无此数据表返回True，否则返回False</returns>
        public static Boolean ExclusiveString(PersistentClass per, string FieldName, string Value)
        {
            if (Value != null || Value.Length == 0) { return true; }
            string table = per.ClassInfo.TableName;

            IDbCommand command;
            command = per.Session.Connection.CreateCommand();
            //是否有该表
            command.CommandText = String.Format("select count(*) from INFORMATION_SCHEMA.TABLES where TABLE_NAME ='{0}' and TABLE_TYPE in ('BASE TABLE', 'VIEW')", table);
            if (Convert.ToInt32(command.ExecuteScalar()) == 0) { return true; }

            //是否唯一
            command.CommandText = String.Format("SELECT Count(*) From [{0}] Where Oid<>{1} and [{2}]='{3}' ", table, per.OID, FieldName, Value);
            return (Convert.ToInt32(command.ExecuteScalar()) == 0);
        }

        /// <summary>
        /// 根据自定义SQL语句检查业务对象（数据表）的唯一性
        /// </summary>
        /// <param name="ObjectType">业务对象类型</param>
        /// <param name="Where">SQL 的 Where 语句</param>
        /// <param name="theSession">Session</param>
        /// <returns>若是唯一或无此数据表返回True，否则返回False</returns>
        public static Boolean ExclusiveByWhere(Type ObjectType, string Where, Session theSession = null)
        {
            XPClassInfo cli;
            if (theSession == null) { theSession = Session.DefaultSession; }

            cli = theSession.Dictionary.GetClassInfo(ObjectType);

            return ExclusiveByWhere(cli, Where);
        }

        /// <summary>
        /// 根据自定义SQL语句检查业务对象（数据表）的唯一性
        /// </summary>
        /// <param name="ObjectInfo">业务对象类型的XPClassInfo</param>
        /// <param name="Where">SQL 的 Where 语句</param>
        /// <param name="theSession">Session</param>
        /// <returns>若是唯一或无此数据表返回True，否则返回False</returns>
        public static Boolean ExclusiveByWhere(XPClassInfo ObjectInfo, string Where, Session theSession = null)
        {
            if (ObjectInfo == null) { return true; }
            string table = ObjectInfo.TableName;
            if (theSession == null) { theSession = Session.DefaultSession; }

            IDbCommand command;
            command = theSession.Connection.CreateCommand();
            //是否有该表
            command.CommandText = String.Format("select count(*) from INFORMATION_SCHEMA.TABLES where TABLE_NAME ='{0}' and TABLE_TYPE in ('BASE TABLE', 'VIEW')", table);
            if (Convert.ToInt32(command.ExecuteScalar()) == 0) { return true; }

            //是否唯一
            command.CommandText = String.Format("SELECT Count(*) From [{0}] Where {1} ", table, Where);
            return (Convert.ToInt32(command.ExecuteScalar()) == 0);
        }
    }
}
